import { HTTP } from "../../../utils/http"
import api from '../../../utils/api.js'
import { countdownTimer } from "../../../utils/util"
import { config } from '../../../utils/config'

let http = new HTTP()

Page({
    data: {
        group_id: 0,
        joinDlgShow: false, // 弹窗显示控制变量
        explainDlgShow: false,
        imgUrl: config.imgUrl,
        user_list: [],
        user_count: 0,
        group: {},
        product: {},
        shop: {},
        joined: false,
        interval_handle: null
    },

    onLoad(options) {
        this.data.group_id = options.group_id;
    },

    onShow() {
        if (this.data.group_id) {
            let _that = this;
            http.request({
                url: api.groupDetail,
                data: { id: _that.data.group_id },
                method: "get",
                success: function (res) {
                    res.data.groupInfo.timer = countdownTimer(res.data.groupInfo.expired_at);
                    _that.setData({
                        user_list: res.data.gorder,
                        user_count: res.data.gorder.length ? parseInt(res.data.gorder.length) : 0,
                        group: res.data.groupInfo,
                        product: res.data.product,
                        shop: res.data.shop,
                        joined: res.data.myPartIn,
                    })
                    _that.interval_handle = setInterval(function () {
                        _that.data.group.timer
                            = countdownTimer(_that.data.group.expired_at)
                        _that.setData({
                            group: _that.data.group
                        })
                    }, 1000);
                }
            })
        }
    },

    goToOrderDetail() {
        this.joinDlgToggle();
        // 设置缓存数据，下个页面使用
        wx.setStorageSync('group_info', {
            group: this.data.group,
            product: this.data.product,
            shop: this.data.shop,
        });
        return wx.navigateTo({
            url: '/pages/group-buy/order-confirm/index',
        });
    },

    goUserList() {
        // 设置用户列表信息进缓存
        wx.setStorageSync('group_user_list', this.data.user_list);
        return wx.navigateTo({
            url: '/pages/group-buy/gb-user-list/index',
        });
    },

    joinDlgToggle() {
        this.setData({
            joinDlgShow: !this.data.joinDlgShow
        })
    },

    explainDlgToggle() {
        this.setData({
            explainDlgShow: !this.data.explainDlgShow
        })
    }
})